from modelscope.pipelines import pipeline
from modelscope.utils.constant import Tasks
from modelscope.utils.cv.image_utils import panoptic_seg_masks_to_image
from modelscope.outputs import OutputKeys
import cv2
#pip install -U openmim
#pip install mmengine -i https://pypi.tuna.tsinghua.edu.cn/simple
#pip install "mmcv>=2.0.0" -i https://pypi.tuna.tsinghua.edu.cn/simple
    
from ApiBase import apiBase

try:
    input_url = apiBase.argv(1,"/data/bzmwork/softrobot/llm/codeqwen-7b/txt/ocr_recognition.jpg")
    output_url = apiBase.argv(2,"/data/bzmwork/softrobot/llm/codeqwen-7b/txt/llmpiccut.jpg")
    
    segmentor = pipeline(Tasks.image_segmentation, model='damo/cv_swinL_panoptic-segmentation_cocopan')
    #input_url = 'https://modelscope.oss-cn-beijing.aliyuncs.com/test/images/image_panoptic_segmentation.jpg'
    result = segmentor(input_url)
    draw_img = panoptic_seg_masks_to_image(result[OutputKeys.MASKS])
    #cv2.imwrite('result.jpg', draw_img)
    cv2.imwrite(output_url, draw_img)
finally:    
    apiBase.close()